//
//  MineViewController.m
//  FD
//
//  Created by Yasin on 2018/10/10.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "MineViewController.h"
//view
#import "MineHeaderView.h"
//controller
#import "MineJiFenViewController.h"
//#import "MineShouCangController.h" //11-30 不要了
#import "MySettingController.h"
#import "PersonInfoController.h"//个人信息页面
#import "MyManageAddressViewController.h" //收货地址管理
#import "MyFadeBackViewController.h" //意见反馈
#import "MyKaQuanViewController.h" //我的卡券
#import "FDMsgViewController.h" //11-30 复用
#import "MyOnlineCustomServiceController.h" //在线客服
#import "FDTabBarController.h" //用于切换根视图控制器

#import "FDNavigationViewController.h"

//model
#import "MineModel.h"

@interface MineCell : UITableViewCell
@end

@implementation MineCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setFrame:(CGRect)frame {
    frame.size.height -= 1;
    [super setFrame:frame];
}
@end

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource,
MineHeaderViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) MineHeaderView *headerView;
@property (nonatomic, strong) NSArray <NSArray *> *sectionArr;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.tableView.frame = self.view.bounds;
    
    //设置表头、表尾
    CGFloat kScreenW = [UIScreen mainScreen].bounds.size.width;
    self.headerView.frame = CGRectMake(0, 0, kScreenW, 180 + 80);
    self.tableView.tableHeaderView = self.headerView;
  
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
    
    //请求数据
    [self requestMineData];
    
    [self __internationalization]; //国际化
    
    
    //注册通知
    //消息推送通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivePushMsgNotification:) name:kPUSHMSGNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(homeVCClickRedDot) name:kClickHomeRedNotification object:nil];
}
- (void)receivePushMsgNotification:(NSNotification *)notifi {

    [self.headerView showRedDot];
}
- (void)homeVCClickRedDot{
    [self.headerView hideRedDot];
}


- (void)__internationalization {
//    self.dataArray = @[@"我的订单",@"个人资料",@"配送地址",@"设置",@"在线客服",@"意见反馈"].mutableCopy;
    
  
    
    NSString *myorderstr = NSLocalizedString(My_MyOrder, @"我的订单");
    NSString *personInfoStr = NSLocalizedString(My_personal_info, @"个人资料");
    NSString *address = NSLocalizedString(My_distribute_address, @"配送地址");
    NSString *setting = NSLocalizedString(My_setting, @"设置");
    NSString *kefuStr = NSLocalizedString(My_online_service, @"在线客服");
    NSString *feedbackStr = NSLocalizedString(My_FeedBack, @"意见反馈");

    //第一个分区
    NSArray *arr1 = @[myorderstr,personInfoStr,address];
    //第二个分区
    NSArray *arr2 = @[setting,kefuStr,feedbackStr];
    
    self.sectionArr = @[arr1,arr2];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    //默认隐藏导航条
    [self.navigationController setNavigationBarHidden:YES];
    
    //重新刷新一下页面
    [self requestMineData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //视图将要消失之前隐藏导航条
    [self.navigationController setNavigationBarHidden:NO];
    
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    //视图将要消失之前隐藏导航条
    [self.navigationController setNavigationBarHidden:NO];
}

#pragma mark - getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        //代理
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //注册cell
        [_tableView registerClass:[MineCell class] forCellReuseIdentifier:@"MineCell"];
        
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = kMainLightGrayColor;
    }
    return _tableView;
}
- (MineHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[MineHeaderView alloc] init];
        //设置代理
        _headerView.delegate = self;
    }
    return _headerView;
}

#pragma mark - 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sectionArr[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineCell"];
    
    //根据section取出小数组
    NSArray *rowArr = self.sectionArr[indexPath.section];
    cell.textLabel.text = rowArr[indexPath.row];

    cell.textLabel.font = [UIFont systemFontOfSize:16.0*kScaleFit]; //等比缩放
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *lightgrayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    lightgrayView.backgroundColor = kMainLightGrayColor;
    return lightgrayView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}


#pragma mark - 代理 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:{ //0我的订单
                UIApplication *app = [UIApplication sharedApplication];
                FDTabBarController *tabVC = [FDTabBarController shareFDTabarVC];
                tabVC.selectedIndex = 2;
                app.keyWindow.rootViewController = tabVC;
            } break;
                
            case 1: { //1个人资料
                 [self gotoPersonVC];
            }
                break;
            case 2:{ //2送货地址
                [self gotoMyAddrssVC];
            }break;
                
            default:
                break;
        }
    }else {
        switch (indexPath.row) {
            case 0: { //3 设置
                [self gotoSettingVC];

            } break;
            case 1: { //4在线客服
                MyOnlineCustomServiceController *customServiceVC = [[MyOnlineCustomServiceController alloc] init];
                [self.navigationController pushViewController:customServiceVC animated:YES];
 
            }break;
            case 2:{ //5.意见反馈
                [self gotoFadeBackVC];

            }break;
                
            default:
                break;
        }
    }
}



//点击cell 事件 & 页面跳转
- (void)gotoPersonVC {
    PersonInfoController *infoVC = [[PersonInfoController alloc] init];
    [self.navigationController pushViewController:infoVC animated:YES];
}
- (void)gotoMyAddrssVC {
    MyManageAddressViewController *addressVC = [[MyManageAddressViewController alloc] init];
    [self.navigationController pushViewController:addressVC animated:YES];
}
- (void)gotoSettingVC {
    MySettingController *settingVC = [[MySettingController alloc] init];
    
    [self.navigationController pushViewController:settingVC animated:YES];
}

- (void)gotoFadeBackVC {
    MyFadeBackViewController *fadebackVC = [[MyFadeBackViewController alloc] init];
    [self.navigationController pushViewController:fadebackVC animated:YES];
}

#pragma mark - 代理 MineHeaderViewDelegate
- (void)didClickSettingAction {
    [self gotoSettingVC];
}

//积分了
- (void)didClickMyJiFenAction {
    MineJiFenViewController *jiFenVC = [[MineJiFenViewController alloc] init];
    
    [self.navigationController pushViewController:jiFenVC animated:YES];
}

- (void)didClickMyKaQuanAction {
    MyKaQuanViewController *kaquanVC = [[MyKaQuanViewController alloc] init];
    [self.navigationController pushViewController:kaquanVC animated:YES];
}


//点击了消息
- (void)didClickMsgAction {
    
    [self.headerView hideRedDot];
    [[NSNotificationCenter defaultCenter] postNotificationName:kClickMineRedNotification object:nil]; //2019-2-20
        
    FDMsgViewController *msgVC = [[FDMsgViewController alloc] init];
    [self.navigationController pushViewController:msgVC animated:YES];
}

- (void)didClickAvatorAction {
    [self gotoPersonVC];
}

#pragma mark - 调接口
- (void)requestMineData {
    [FDHttpAPi requestUserMySuccessBlock:^(id responseObjc) {
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:@"SUCCESS"]) {
            
            //解析数据
            NSDictionary *dataDic = responseObjc[@"data"];
            
            MineModel *model = [MineModel mj_objectWithKeyValues:dataDic];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //重新赋值
                self.headerView.myModel = model;
            });
            
        }else if ([event isEqualToString:@"ERROR"]) { //如果发生错误就toast
            [JRToast showWithText:responseObjc[@"describe"]];
        }else if ([event isEqualToString:responseObjc[@"EXCEPTION"]]){ //发生异常
            NSLog(@"%s---%d:异常样了",__FUNCTION__,__LINE__);
        }else { //其他
            NSLog(@"其他");
        }
        
    } failBlock:^(NSError *error) {
        NSLog(@"发生戳误了");
    }];
}


@end
